package wk.demo.tool;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.Locale;


public class BrightnessTools {
    private Context mcontext ;
    private TextView extInfo ;
    private int currentBrghtnewss =125 ;

    public BrightnessTools(Context mcontext ,TextView extInfo ){
        this.mcontext =mcontext ;
        this.extInfo = extInfo ;
    }

    public int getCurrentBrghtnewss() {
        return currentBrghtnewss;
    }

    public void setCurrentBrghtnewss(int currentBrghtnewss) {
        this.currentBrghtnewss = currentBrghtnewss;
    }

    public void changeBrightness(boolean isAdd){
        boolean isAuto =isAutoBrightness(mcontext) ;
        if(isAuto){
            stopAutoBrightness(mcontext ) ;
        }

        currentBrghtnewss = isAdd?   (currentBrghtnewss+1) : (currentBrghtnewss-1) ;
        currentBrghtnewss =  currentBrghtnewss>=255 ? 255 : currentBrghtnewss ;
        currentBrghtnewss =  currentBrghtnewss <=0 ? 0 : currentBrghtnewss ;
        setBrightness(  (Activity) mcontext ,currentBrghtnewss);

        extInfo.setVisibility(View.VISIBLE);
        extInfo.setText( String.format(Locale.getDefault() ,"亮度:%d" ,currentBrghtnewss)  );
    }

    /**
     * 判断是否开启了自动亮度调节
     */

    private boolean isAutoBrightness(Context context) {
        ContentResolver resolver = context.getContentResolver();
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return automicBrightness;
    }



    /**
     *  获取 系统屏幕亮度值 ，范围（0-255）
     * **/
    public int getScreenBrightness(Context context  ) {
        ContentResolver contentResolver = context.getContentResolver();
        return Settings.System.getInt(contentResolver,  Settings.System.SCREEN_BRIGHTNESS, currentBrghtnewss);
    }


    /**
     * 设置亮度 。只会影响当前APP 。
     */
    private void setBrightness(Activity activity, int brightness) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.screenBrightness = (float) brightness * (1f / 255f);  //  0 to 1
        activity.getWindow().setAttributes(lp);
    }

    /**
     *  修改系统 屏幕亮度 ， 会影响所有APP。
     *  需要系统权限。需要引导用户去开启。
     *  <uses-permission android:name="android.permission.WRITE_SETTINGS" />
     */
    private void saveBrightness( Context context   , int brightness) {
        ContentResolver resolver = context.getContentResolver();
        Uri uri = android.provider.Settings.System   .getUriFor(Settings.System.SCREEN_BRIGHTNESS);
        android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS,   brightness);
        resolver.notifyChange(uri, null);
    }


    /**
     * 停止自动亮度调节
     */
    private  void stopAutoBrightness(Context context) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }


    /**
     * 开启亮度自动调节
     */
    private void startAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }



}
